#include "../include/commandStack.h"
#include <pwd.h>
//该函数作用是：取得绝对路径
int get_homedir(char buf[])
{
    strcpy(buf,"/home/");
    uid_t uid = getuid();
    struct passwd *pwd = getpwuid(uid);
    strcat(buf,pwd->pw_name);
    strcat(buf,"/zhangsan");
    /* printf("%s\n",buf); */
    return 0;
}

int trans_res(int netfd,const char res[])
{
    int type = 0;
    send(netfd,&type,sizeof(type),0);
    int length = strlen(res)+1;
    send(netfd,&length,sizeof(length),0);
    send(netfd,res,length,0);

    return 0;
}
int send_blank(int netfd)
{
    int type = 0;
    send(netfd,&type,sizeof(type),0);
    int length = 0;
    send(netfd,&length,sizeof(length),0);
    return 0;
}
// home/niu/zhangsan/dir1/dir2
int do_ls(int netfd,commandStack_t * stack,commandStack_t * cdStack){
    printf("stack:");
    print_stack(stack);
    printf("cdstack:");
    print_stack(cdStack);
    char buf[4096]={0};
    //开始获取绝对路径
    
    //先把绝对路径拼上
    //开始遍历并且拼接虚拟路径
    /* if(node->data == NULL){ */
        assemble_stack(cdStack,buf);
        printf("buf=%s\n",buf);
        DIR* dirp=opendir(buf);
        ERROR_CHECK(dirp,NULL,"opendir error");
        char buf1[4096]={0};
        struct dirent *pdirent;
        while((pdirent=readdir(dirp))!=NULL){
            if(strcmp(pdirent->d_name,".") != 0 && strcmp(pdirent->d_name,"..") != 0)
            {
                strcat(buf1,pdirent->d_name);
                strcat(buf1," ");
            }

        }
        trans_res(netfd,buf1);
        /* send_blank(netfd); */
        /* return 0; */
    /* } */
    /* bzero(buf,4096); */
    /* assemble_stack(stack,buf); */
    /* //打印一下绝对路径 */
    /* printf("buf=%s\n",buf); */
    /* DIR* dirp=opendir(buf); */
    /* ERROR_CHECK(dirp,NULL,"opendir error"); */
    /* char buf1[4096]={0}; */
    /* struct dirent *pdirent; */
    /* while((pdirent=readdir(dirp))!=NULL){ */
    /*     if(strcmp(pdirent->d_name,".") != 0 && strcmp(pdirent->d_name,"..") != 0) */
    /*     { */

    /*         strcat(buf1,pdirent->d_name); */
    /*         strcat(buf1," "); */
    /*     } */

    /* } */

    /* trans_res(netfd,buf1); */
    /* printf("do_ls after:stack:"); */
    /* print_stack(stack); */
    /* printf("cdstack:"); */
    /* print_stack(cdStack); */
    /* /1* send_blank(netfd); *1/ */
    //tranfile buf就行了
    /* trans_res(netfd,"12345789\n"); */
    return 0;
}

int do_pwd(int netfd,commandStack_t *stack){
    char buf[1024] = {0};
    if(stack->stackSize == 1){
        trans_res(netfd, "/");
    }else{
        char buf1[100];
        get_homedir(buf1);
        int num=strlen(buf1);
        assemble_stack(stack,buf);
        trans_res(netfd, buf+num);

    }
    /* send_blank(netfd); */
    return 0;
}
int do_mkdir(int netfd,commandStack_t *stack)
{
    char path[100] = {0};
    assemble_stack(stack,path);

    int ret = mkdir(path,0777);
    if(ret == -1)
    {
        trans_res(netfd,"无法创建目录\n");
        send_blank(netfd);
        return 0;
    }
    trans_res(netfd,"目录创建成功\n");
    /* send_blank(netfd); */
    return 0;
}
int do_rmdir(int netfd,commandStack_t *stack)
{
    char path[1000] = {0};
    assemble_stack(stack,path);
    DIR *dirp = opendir(path);
    ERROR_CHECK(dirp,NULL,"opendir");

    struct dirent *pdirent;
    int num = 0;
    while((pdirent = readdir(dirp)) != NULL)
    {
        num++;
    }
    if(num == 2)
    {
        rmdir(path);
        trans_res(netfd,"目录删除成功\n");
    }
    else
    {
        trans_res(netfd,"目录非空\n");
    }
    /* send_blank(netfd); */
    closedir(dirp);
    return 0;
}
int do_cd( int netfd,commandStack_t *stack,commandStack_t *cdStack ){
    printf("stack:");
    print_stack(stack);
    printf("cdstack:");
    print_stack(cdStack);
    if(stack->stackSize == 1 || stack->stackSize == 0)
    {
        while(cdStack->stackSize > 1)
        {
            pop_command_stack(cdStack);
        }
        trans_res(netfd,"回到根目录\n");
        return 0;
    }
    stackNode_t *node = stack->pBottom;
    stackNode_t *cdnode = cdStack->pBottom;
    while(cdnode)
    {
        cdnode = cdnode->next;
        node = node->next;//单个/*的路径
    }
    while(node != NULL){
        // 当前节点为..，返回上一级，出栈
        if((strcmp(node->data,"/..")==0) || (strcmp(node->data,"../")==0) || (strcmp(node->data,"/../")==0))
        {   
            printf("node->data: %s\n", node->data);
            if(node->data[strlen(node->data)-1] == '/')
            {
                node->data[strlen(node->data)-1] = '\0';
            }
            printf("cdstacksize=%d\n",cdStack->stackSize);
            if(cdStack->stackSize==1){
            /* trans_res(netfd,"当前目录已是根目录"); */
            
            }else{
            pop_command_stack(cdStack);
            //trans_res(netfd,"回退到上一级目录");
            }
        } else if(strcmp(node->data,"/.")==0){
        // 当前节点为.不切换路径
            /* trans_res(netfd,"当前路径"); */
        }
        else{
            //当前节点为正常路径，入栈
            push_command_stack(cdStack,node->data,strlen(node->data));
            /* trans_res(netfd,"目录切换成功\n"); */
        }
        node=node->next;
    }
        trans_res(netfd,"目录切换成功\n");
    printf("after do_cd:stack:");
    print_stack(stack);
    printf("cdstack:");
    print_stack(cdStack);
    return 0;
}
int do_gets(int netfd,commandStack_t *stack)
{
    s_download(netfd,stack);
    trans_res(netfd,"下载成功\n");
    send_blank(netfd);
    return 0;
}
int do_puts(int netfd,commandStack_t *stack)
{
    char buf[1024]={0};
    assemble_stack(stack, buf);
    printf("do puts: buf = %s\n",buf);
    s_recv_up(netfd,stack);
    trans_res(netfd,"上传成功\n");
    send_blank(netfd);
    return 0;
}
